package cn.javabb.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;
import org.springframework.validation.support.BindingAwareModelMap;

/**
 * 缓存key相关的工具类
 *
 */
public class CacheKeyUtil {

    /**
     * 获取方法参数组成的key
     *
     * @param params
     *         参数数组
     */
    public static String getMethodParamsKey(Object... params) {
        if (StringUtils.isEmpty(params)) {
            return "";
        }
        StringBuilder key = new StringBuilder("(");
        for (Object obj : params) {
            if(null == obj){
                key.append("_N_");
                continue;
            }
           // System.out.println("当前参数类型为："+obj.getClass());
            if (obj.getClass().equals(BindingAwareModelMap.class)) {
                continue;
            }
            key.append(JSON.toJSONString(obj).replaceAll("\"", "'"));
        }
        key.append(")");
        return key.toString();
    }

    public static void main(String[] args) {
        JSONObject param = new JSONObject();
        param.put("abc",11);
        param.put("da","aaa");
        param.put("sqlid","dsafasfasfadsfas");
        System.out.println(getMethodParamsKey(param,"dfasfs"));
    }
}
